const mongoose = require('mongoose');

const getTheaters = async ({ cinemasId, curPage, eachPage }) => {
    const result = {
        curPage: ~~curPage,
        eachPage: ~~eachPage,
        total: 0,
        maxPage: 0,
        rows: [],
    }
    const theaterstModel = mongoose.model('theaters');

    result.maxPage = Math.ceil(result.total / result.eachPage);

    const total = await theaterstModel.find({ cinemasId })
    result.total = total.length

    result.rows = await theaterstModel
        .find({ cinemasId })
        .populate('cinemasId')
        .skip((result.curPage - 1) * result.eachPage)
        .limit(result.eachPage)
        .exec();

    return result
}
const delTheaters = async ({ _id }) => {
    mongoose.model("theaters").remove({ _id }, (err, ret) => {
        if (err) {
            return { state: false }
        } else {
            return { state: true }
        }
    })
    await mongoose.model("seats").remove({ theaterId: _id }, (err, ret) => {
        if (err) {
            return { state: false }
        } else {
            return { state: true }
        }
    })
}



const addTheaters = async ({ name, status, cinemasId }) =>
    mongoose.model("theaters").create({ name, status, cinemasId });





const patchTheaters = async ({ _id, name, status, cinemasId }) =>
    mongoose.model("theaters").findByIdAndUpdate(_id, {
        name, status, cinemasId
    }, (err, ret) => {
        err ? console.log("失败", err) : console.log('成功');
    });


module.exports = { getTheaters, delTheaters, addTheaters, patchTheaters }

